package com.qnw.CardGroupManagement;

import android.app.*;
import android.database.*;
import android.database.sqlite.*;
import android.os.*;
import android.text.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.AdapterView.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;

import android.view.View.OnClickListener;
import com.ygo.feihua.R;
import com.ygo.feihua.Gongju;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.qnw.CardGroupManagement.management.KzlistManagement;
import com.ygo.feihua.util.*;
import com.qnw.CardGroupManagement.util.*;
import android.util.*;
import com.qnw.CardGroupManagement.bean.*;

public class F5 extends AppCompatActivity implements OnClickListener
{
	private Spinner sp_jxkb;
	private EditText ed_zkz;
	private EditText ed_ew;
	private EditText ed_fkz;
	private Button bu_zk;

	private Gongju gj;
	private DialogUtils du;
	private KzlistManagement kzm;
	
	//禁限卡表
	private String lflist;
	//数据库
	private String db;
	private int zks,exs,sides;

	private LflistConf lflistConf;

	private List<String> ls;
	public void onClick(View p1)
	{
		switch (p1.getId())
		{
			case R.id.f5_bu_zk:
					final List<DeckType> da=kzm.getType();
					String ss[]=new String[da.size() - 1];
					for (int i=0;i < ss.length;i++)
					{
						ss[i] = da.get(i + 1).getName();
					}

					ListView ll=(ListView) du.dialogl(true, "保存到", ss);
					ll.setOnItemClickListener(new OnItemClickListener(){

							@Override
							public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
							{

								try{
									GroupCard(da.get(p3 + 1).getPath() + new Date().getTime() + ".ydk");
									gj.show("已保存到" + (String)da.get(p3 + 1).getPath() + new Date().getTime() + ".ydk");
									
								}catch (Exception e){
									gj.show("保存失败,原因为"+e);
								}
								du.dis();
							}				
						});
					break;
		}
	}

	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.f5_layout);

		sp_jxkb = (Spinner)findViewById(R.id.f5_sp_jxkb);
		ed_zkz = (EditText)findViewById(R.id.f5_ed_zkz);
		ed_ew = (EditText)findViewById(R.id.f5_ed_ew);
		ed_fkz = (EditText)findViewById(R.id.f5_ed_fkz);
		bu_zk = (Button)findViewById(R.id.f5_bu_zk);
		Toolbar tool=(Toolbar) findViewById(R.id.toolbar);
		kzm=KzlistManagement.getdx();
		
		gj=Gongju.getdx(this);
		du=DialogUtils.getdx(this);
		CgmaZhuye.getType();
		lflist=Gongju.PATH_OURYGO+"lflist.conf";
		db=SharedPreferenceUtil.getCdbpath();
		
		bu_zk.setOnClickListener(this);

		gj.cshToolbar(tool,"随机组卡");
		du.dialogj1(false,"","加载中,请稍等");
		
		new Thread(new Runnable(){

				@Override
				public void run()
				{
					lflistConf=LflistConf.getLflistConf(lflist);	
					
					ls=lflistConf.getNames();
					han.sendEmptyMessage(0);
					// TODO: Implement this method
				}
			}).start();
		 
		
		
	}
	
	Handler han=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);
			switch(msg.what){
				case 0:
					ArrayAdapter<String> adapter=new ArrayAdapter<String>(F5.this, android.R.layout.simple_spinner_dropdown_item, ls);
					sp_jxkb.setAdapter(adapter);
					du.dis();
					break;
			}
			
		}
		
		
	};
	
	private void GroupCard(String path) throws IOException
	{
		String zk,ex,side;
		zk=ed_zkz.getText().toString().trim();
		ex=ed_ew.getText().toString().trim();
		side=ed_fkz.getText().toString().trim();
		
		if(zk.equals("")){
			zks=40;
		}else{
			zks=Integer.parseInt(zk);
		}
		
		if(ex.equals("")){

			exs=40;
		}else{
			exs=Integer.parseInt(ex);
		}
		
		if(side.equals("")){

			sides=40;
		}else{
			sides=Integer.parseInt(side);
		}
		
		String name=sp_jxkb.getSelectedItem().toString();
		Random random=new Random();
		SQLiteDatabase database=SQLiteDatabase.openOrCreateDatabase(db, null);
		List<String> list1=new ArrayList<String>();//主卡组
		List<String> list2=new ArrayList<String>();//额外卡组
		List<String> list3=new ArrayList<String>();//副卡组
		List<String> cards=new ArrayList<String>();//所有卡
		List<String> type=null;//卡片类型
		//获取所有卡片
		Cursor cursir=database.rawQuery("select id from datas", null);
		StringBuilder sb=null;
		while (cursir.moveToNext())
		{
			sb = new StringBuilder(cursir.getString(0));
		/*	while (sb.length() < 8)
			{
				sb.insert(0, "0");
			}*/
			cards.add(sb.toString());
		}
		cursir.close();
		//随机组卡
		while (list1.size() < zks)
		{
			String id=cards.get(random.nextInt(cards.size()));
			if (list1.contains(id))
				continue;
			cursir = database.rawQuery("select type from datas where id=?", new String[]{id});
			cursir.moveToNext();
			type =CardMessage.getCardType(cursir.getInt(0));
			if (type.contains("衍生物") || type.contains("超量") || type.contains("同调") || type.contains("融合") || type.contains("连接"))
				continue;
			cursir.close();
			for (int i=0;i < random.nextInt(lflistConf.get(name, id) + 1);i++)
			{
				if (list1.size() < 40){
					while(id.substring(0,1).equals("0")){
					id=id.substring(1,id.length());
						}
					list1.add(id);
					
				}
			}
		}
		while (list2.size() < exs)
		{
			String id=cards.get(random.nextInt(cards.size()));
			if (list2.contains(id))
				continue;
			cursir = database.rawQuery("select type from datas where id=?", new String[]{id});
			cursir.moveToNext();
			type =CardMessage.getCardType(cursir.getInt(0));
			if (!(type.contains("超量") || type.contains("同调") || type.contains("融合") || type.contains("连接")))
				continue;
			cursir.close();
			for (int i=0;i < random.nextInt(lflistConf.get(name, id) + 1);i++)
			{
				if (list2.size() < 15)
					list2.add(id);
			}
		}
		while (list3.size() < sides)
		{
			String id=cards.get(random.nextInt(cards.size()));
			if (list1.contains(id) || list2.contains(id) || list3.contains(id))
				continue;
			cursir = database.rawQuery("select type from datas where id=?", new String[]{id});
			cursir.moveToNext();
			type =CardMessage.getCardType(cursir.getInt(0));
			if (type.contains("衍生物"))
				continue;
			cursir.close();
			for (int i=0;i < random.nextInt(lflistConf.get(name, id) + 1);i++)
			{
				if (list3.size() < 15)
					list3.add(id);
			}
		}
		database.close();
		//保存到文件
			PrintWriter pw=null;
			String filename=path;
				File f=new File(filename);
			
				pw=new PrintWriter(new FileWriter(f));
				pw.write("#main\n");
				for(String s:list1)
					pw.write(s+"\n");
				pw.write("#extra\n");
				for(String s:list2)
					pw.write(s+"\n");
				pw.write("!side\n");
				for(String s:list3)
					pw.write(s+"\n");
					
		if(pw!=null)
			pw.close();
			
	}
	
	
}
